package com.skymxc.demo.explorer.adapter;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

import com.skymxc.demo.explorer.TabFragment;
import com.skymxc.demo.explorer.entity.PageTab;

import java.util.LinkedList;
import java.util.List;

/**
 * Created by sky-mxc
 */

public class FileListPagerAdapter extends FragmentPagerAdapter {

    //数据集合
    private List<PageTab> tabs;
    private static final String TAG = "FileListPagerAdapter";
    private LinkedList<Long> ids = new LinkedList<>();

    public FileListPagerAdapter(FragmentManager fm, List<PageTab> tabs) {
        super(fm);
        this.tabs = tabs;
        //接受传入的数据集合
        createIds(tabs);
    }

    private void createIds(List<PageTab> tabs) {
        for (int i=0;i<tabs.size();i++){
            ids.add(new Long(i));
        }
    }


    @Override
    public Fragment getItem(int position) {
       PageTab  pageTab = tabs.get(position);
        TabFragment tab = new TabFragment();
      // Log.e(TAG,"====getItem===新创建时的tag:=="+tab.getTag());
        tab.getArguments().putSerializable("tab",pageTab);
        //取出数据 新建Fragment
        return tab;
    }

    @Override
    public int getCount() {
        return tabs==null?0:tabs.size();
    }

    @Override
    public CharSequence getPageTitle(int position) {

        return tabs.get(position).getName();
    }

    @Override
    public int getItemPosition(Object object) {
        return POSITION_NONE;
    }




    @Override
    public long getItemId(int position) {
        return ids.get(position);
    }


    /**
     * 添加根标签
     * @param tab
     */
    public void addTab(PageTab tab){
        tabs.add(tab);
        Long id = new Long(0);
        for (long l :ids){
            if (id<l){
                id=l;
            }
        }
        ids.add(id+1);
        notifyDataSetChanged();
    }

    /**
     * 移除根标签
     * @param position
     */
    public void removeTab(int position){
       PageTab tab= tabs.remove(position);
        tab.delete();
        ids.remove(position);
        notifyDataSetChanged();
    }


}
